home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Game Programming in C++ - Start to Finish
/
GameProgrammingS.iso
/
Peon
/
PeonSDK-Win32-1.0.0.exe
/
{app}
/
PeonMain
/
source
/
EngineCore.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-11-30
|
5KB
|
258 lines
#include "FileLogger.h"
#include "IniConfigReader.h"
#include "SceneRenderer.h"
#include "IApplication.h"
#include "EngineCore.h"
namespace peon
{
//-----------------------------------------------------------------------
template<> EngineCore* ISingleton<EngineCore>::ms_Singleton = 0;
EngineCore* EngineCore::getSingletonPtr(void)
{
return ms_Singleton;
}
EngineCore& EngineCore::getSingleton(void)
{
assert( ms_Singleton );
return ( *ms_Singleton );
}
EngineCore::EngineCore()
{
m_pApplication = NULL;
m_pConfig = NULL;
m_pVideoDevice = NULL;
m_fps = 0.0f;
}
EngineCore::~EngineCore()
{
}
bool EngineCore::loadEngine(const String& strWindowTitle, const String& strIniConfig )
{
#if defined( WIN32 )
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
#endif
new FileLogger( PEON_LOG_DEBUG );
if(!FileLogger::getSingleton().openLogStream("PeonMain.log"))
{
OutputDebugString("failed to open file\n");
unloadEngine();
return false;
}
//start up SDL
if(SDL_Init( SDL_INIT_EVERYTHING ) < 0)
{
FileLogger::getSingleton().logFatal("EngineCore", "Failed to load SDL. Exiting");
unloadEngine();
return false;
}
m_pConfig = new IniConfigReader( strIniConfig );
if(NULL == m_pConfig)
{
FileLogger::getSingleton().logFatal("EngineCore", "Failed to load INI file. Exiting");
unloadEngine();
return false;
}
m_pVideoDevice = new SceneRenderer();
if(!m_pVideoDevice->loadDevice( m_pConfig ))
{
FileLogger::getSingleton().logFatal("EngineCore", "Failed to load graphics renderer. Exiting");
unloadEngine();
return false;
}
SDL_WM_SetCaption(strWindowTitle.c_str(), strWindowTitle.c_str());
//Initialize the AudioEngine instance
new AudioEngine();
if(!AudioEngine::getSingleton().loadEngine( m_pConfig ))
{
FileLogger::getSingleton().logInfo("EngineCore", "Failed to initialize sound device. Disabling audio support");
}
new InputEngine();
if(!InputEngine::getSingleton().loadEngine( m_pConfig ))
{
FileLogger::getSingleton().logFatal("EngineCore", "Failed to initialize input device(s). Exiting");
unloadEngine();
return false;
}
return true;
}
void EngineCore::unloadEngine()
{
FileLogger::getSingleton().logDebug("EngineCore", "Shutting down Peon");
if( m_pApplication )
{
m_pApplication->onUnloadWorld();
m_pApplication->unloadStates();
//m_pApplication->getSingleton().onUnloadWorld();
//m_pApplication->getSingleton().unloadStates();
//delete IApplication::getSingletonPtr();
PEON_DELETE( m_pApplication );
}
//cleanup the AudioEngine instance
delete AudioEngine::getSingletonPtr();
delete InputEngine::getSingletonPtr();
PEON_DELETE( m_pVideoDevice );
PEON_DELETE( m_pConfig );
SDL_Quit();
delete FileLogger::getSingletonPtr();
}
int EngineCore::runEngine()
{
m_oTimer.start();
bool done = false;
SDL_Event event;
while(! done)
{
while( SDL_PollEvent(&event) )
{
switch ( event.type )
{
case SDL_KEYDOWN:
case SDL_KEYUP:
//first test for the ESCAPE key
if(event.key.keysym.sym == SDLK_ESCAPE)
{
//quit the app
done = true;
break;
}
if(m_pApplication)
m_pApplication->onKeyEvent(&event.key);
break;
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
if(m_pApplication)
m_pApplication->onMouseEvent( &event );
break;
case SDL_QUIT :
done = true;
break;
default:
break;
}
}
float elapsed_time = m_oTimer.getElapsedTime();
if(m_pApplication)
{
m_pApplication->onUpdateWorld( elapsed_time );
}
if(m_pVideoDevice->clearDevice())
{
if(m_pApplication)
{
m_pApplication->onRenderWorld();
}
m_pVideoDevice->flipDevice();
updateFPS();
}
}
unloadEngine();
return 0;
}
bool EngineCore::setApplication( IApplication* pApp )
{
if( NULL == pApp )
return false;
if(!pApp->onLoadWorld())
{
FileLogger::getSingleton().logError("EngineCore", "Error on IApplication::onLoad()");
return false;
}
m_pApplication = pApp;
return true;
}
void EngineCore::updateFPS()
{
static float fLastTime = 0.0f;
static DWORD dwFrames = 0L;
float fTime = m_oTimer.getAbsoluteTime();
++dwFrames;
// Update the scene stats once per second
if( fTime - fLastTime > 1.0f )
{
m_fps = dwFrames / (fTime - fLastTime);
fLastTime = fTime;
dwFrames = 0L;
}
}
}